package com.zzl.alligator.framework.security.hanlder;

import cn.hutool.http.HttpStatus;
import com.zzl.alligator.common.util.JsonUtils;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zhouziliang
 */
@Component
@Slf4j
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse resp, AccessDeniedException e) throws IOException, ServletException {
        log.error("权限不足，", e);
        PrintWriter out = resp.getWriter();
        out.write(JsonUtils.toJsonString(JsonResult.error(HttpStatus.HTTP_FORBIDDEN, "权限不足，请联系管理员!")));
        out.flush();
        out.close();
    }
}